package top.infopub.mgr.common.aop;


import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import top.infopub.concurrent.ExecutorManager;
import top.infopub.model.sys.ErrorLog;
import top.infopub.tdp.util.ErrorLogUtil;
import top.infopub.zookeeper.confclient.ContextUtils;


/**
 * error异常日志入表 service层  </br>
 * 参见：ExceptionHandlerFilter， controller层error 此处不再切service
 * @author Awoke
 * @version 2018年9月27日
 * @see ErrorLogAop
 * @since
 */
//@Component
//@Aspect
public class ErrorLogAop {

    private static final Logger logger = LoggerFactory.getLogger(ErrorLogAop.class);

    //@AfterThrowing(pointcut = "execution (* top.infopub..*.service.impl.*Impl.*(..))", throwing = "ex")
    public void afterThrowing(Throwable ex) {
        logger.error("service error--", ex);
        // 参见：ExceptionHandlerFilter， controller层error
        final ErrorLog log = new ErrorLog();
        log.setSprojectname(ContextUtils.PROJECT_NAME);
        log.setSerrorname(ex.getClass().getName());
        log.setSerrorinfo(ex.getMessage());
        log.setToperateDate(new Date());
        // 确保insert-log不被外部事务回滚掉
        ExecutorManager.getInstance().execute(new Runnable(){
            @Override
            public void run() {
                ErrorLogUtil.log(log);
            }
        });
    }

}
